package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.application.sistercityapplication.cityinformationpage.CityInformationListAdapter;
import com.application.sistercityapplication.dto.InformationMasterDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

@SuppressLint("ValidFragment")
public class GeneralInfoFragment extends Fragment {
	private ProgressBar pBar;
	private View v,v2;
	private LayoutInflater inflater2;
	private FrameLayout mainLayout;
	private int cId;
	private ImageView menuToggle;
	private SlidingMenu sm;
	private String country_name;
	public GeneralInfoFragment(int country_id,SlidingMenu sm,String country_name){
		this.cId = country_id;
		this.sm = sm;
		this.country_name=country_name;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		v = inflater.inflate(R.layout.new_activity_city_information_main, container,false);

		mainLayout = (FrameLayout) v.findViewById(R.id.frameForInflate);
		inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v2 = inflater2.inflate(R.layout.general_info_list, null);
		ImageView back_icon = (ImageView) v2.findViewById(R.id.back_icon);
//		back_icon.setVisibility(View.INVISIBLE);
		menuToggle = (ImageView) v2.findViewById(R.id.menuToggle);
		menuToggle.setVisibility(View.INVISIBLE);
		pBar = (ProgressBar) v2.findViewById(R.id.progress);
		mainLayout.addView(v2);
		
		List<InformationMasterDto> listNews = new ArrayList<InformationMasterDto>();
		new DownLoadInfoTask(getActivity().getApplicationContext(),listNews,getActivity(),pBar).execute();
		menuToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sm.toggle();
			}
		});
		back_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Fragment newContent = new MainPageFragment(cId,country_name,sm);
				switchFragment(newContent);
			}
		});
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	
	public class DownLoadInfoTask extends AsyncTask<String, Void, List<InformationMasterDto>> {
		ProgressDialog pDialog;
		private Context context;
		private List<InformationMasterDto> listInfo;
		public Activity _activity;
		private ProgressBar _resid;

		public DownLoadInfoTask(Context context, List<InformationMasterDto> view,Activity act,ProgressBar resid) {
			super();
			this.context = context;
			this.listInfo = view;
			this._activity = act;
			this._resid = resid;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			_resid.setVisibility(View.VISIBLE);
		}

		@Override
		protected List<InformationMasterDto> doInBackground(String... params) {
			listInfo = JSONParserForGetList.getInstance().getInformations(cId+"");

			return listInfo;
		}

		@Override
		protected void onPostExecute(List<InformationMasterDto> result) {
			super.onPostExecute(result);
			menuToggle.setVisibility(View.VISIBLE);
			_resid.setVisibility(View.GONE);
			if (result != null) {
				ListView informationListView = (ListView) v2.findViewById(R.id.informationListView);
				CityInformationListAdapter adp = new CityInformationListAdapter(listInfo, getActivity().getApplicationContext(),getActivity(), mainLayout,cId+"");
				informationListView.setAdapter(adp);
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG)).show();
			}

		}
	}
}
